package com.example.servlet.jiao_test;

import com.example.dao.UsernameDao;
import com.example.dao.UsernameImpl;
import com.example.entity.UserName;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "register", value = "/register")
public class AddUser extends HttpServlet {
    public void init() {
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("user_password");
        String rePassWord = request.getParameter("rePassWord");
        HttpSession session = request.getSession();

        if (username != null && !"".equals(username)) {
            UsernameDao user = new UsernameImpl();
            int users = user.getregister(username, password);
            if (rePassWord.equals(password)) {
                request.setAttribute("mage", "注册成功,请登录");
                request.getRequestDispatcher("page/login.jsp").forward(request, response);
            } else {
                request.setAttribute("no", "两次密码必须一致,请重新输入");
                request.getRequestDispatcher("page/register.jsp").forward(request, response);
            }
        } else {
            request.setAttribute("no", "输入不能为空,请重新输入");
            request.getRequestDispatcher("page/register.jsp").forward(request, response);
        }
    }

    public void destroy() {
    }
}
